#include "config.h"

#include <clx/libECM.h>
#include <clx/ecm_list.h>

#include "blockcache.h"

static LIST_HEAD(mru_list);

typedef struct {
	bcache_t	*bcache;
	struct list_head	list;
} priv_t;

static const char *
mru_init(ecm_uint32 cache_blksize, ecm_uint32 n_caches)
{
	return "MRU";
}

static void
mru_create(bcache_t *bcache)
{
	priv_t	*priv;

	priv = (priv_t *)ecm_malloc(sizeof(priv_t));
	bcache->priv = priv;

	priv->bcache = bcache;
	list_add(&priv->list, &mru_list);
}

static void 
mru_hit(bcache_t *bcache)
{
	priv_t	*priv = (priv_t *)bcache->priv;
	
	list_del_init(&priv->list);
}

static bcache_t *
mru_replace(ecm_uint32 mid, ecm_uint32 bid)
{
	priv_t	*priv;

	if (list_empty(&mru_list))
		return NULL;

	priv = list_entry(mru_list.next, priv_t, list);
	list_del_init(&priv->list);
	return priv->bcache;
}

static void
mru_put(ecm_uint32 sid, bcache_t *bcache)
{
	priv_t	*priv = (priv_t *)bcache->priv;

	list_add(&priv->list, &mru_list);
}

static void
mru_access(ecm_uint32 sid, ecm_uint32 mid, ecm_uint32 bid)
{
}

static void
mru_leave(ecm_uint32 sid)
{
}

static void
mru_add_media(ecm_uint32 bid_max)
{
}

static void
mru_fini(void)
{
}

blockcache_op_t	bcache_op = {
	mru_init,
	NULL,
	NULL,
	mru_add_media,
	mru_create,
	mru_hit,
	mru_replace,
	mru_put,

	mru_access,
	mru_leave,
	mru_fini
};
